package com.example.lzwphoto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lzwphoto.dto.CategoryDto;
import com.example.lzwphoto.enity.Category;
import com.example.lzwphoto.enity.Photo;
import com.example.lzwphoto.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Desc: TODO
 * @Author: LvKun
 * @Date: 2024-02-05
 * @Version: 1.0
 */

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements IService<Category> {

    @Autowired
    private PhotoServiceImpl photoService;

    @Transactional
    public void delWithCateGory (Long id) {

        List<Category> list = lambdaQuery().eq(Category::getParentId, id).list();

        List<Long> collect = list.stream().map(Category::getId).collect(Collectors.toList());

        collect.add(id);

        List<Long> photos = new ArrayList<>();

        collect.forEach(item -> {
            List<Long> photoItems = photoService.list().stream().filter(photo -> photo.getCategoryId().equals(item)).map(mp -> {
                photoService.delPhotoByServiceMachinePath(mp.getId());
                return  mp.getId();
            }).collect(Collectors.toList());
            photos.addAll(photoItems);
        });



        photoService.removeByIds(photos);

        this.removeByIds(collect);

    }

}
